<template>
  <div>

      <div :class="['parse', store.className ? store.className : '']">
        <el-form
          :model="store.models"
          ref="form"
          :label-position="store.root.config.labelPosition"
          :label-width="store.root.config.labelWidth + 'px'"
        >
          <template v-for="(el, i) in store.root.list">
            <widget
              :store="store"
              :element="el"
              :index="i"
              :key="el.key"
              :data.sync="store.root.list"
            ></widget>
          </template>

          <div>
            <template v-for="(el, i) in store.root.blackList">
              <widget
                :store="store"
                :element="el"
                :index="i"
                :key="el.key"
                :data.sync="store.root.blackList"
              ></widget>
            </template>
          </div>
        </el-form>
      </div>

  </div>
</template>

<script>
export default {
  inject: ["getStore"],
  name: "parse",
  provide() {
    return {
      model: "preview", //界面
    };
  },
  computed: {
    store() {
      return this.getStore();
    },
  },
  mounted() {
    let _this = this;
    this.store.eventCenter.on("submit", (form) => {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // alert( JSON.stringify(this.store.lifeCycle.beforeSubmit()) )
          this.$message({
            showClose: true,
            message: "恭喜你，这是一条成功消息",
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: "错了哦，这是一条错误消息",
            type: "error",
          });
        }
      });
    });

    this.store.controller.verify = function () {
      return new Promise((resolve) => {
        _this.$refs["form"].validate((valid) => {
          if (valid) {
            resolve({
              isSuccess: true,
            });
          } else {
            resolve({
              isSuccess: false,
            });
          }
        });
      });
    };

    this.store.controller.resetFields = this.$refs["form"].resetFields.bind(this);

    this.store.controller.submit=this.submit.bind(this);
  },
  methods: {
    submit() {
      let _this = this;
      return new Promise((resolve) => {
        _this.$refs["form"].validate((valid) => {
          if (valid) {
            setTimeout(()=>{
             resolve({
              isSuccess: true,
              result:null
            });
            },500)
          } else {
             resolve({
              isSuccess: false,
              errorMessage:'校验失败'
            });
          }
        });
      });
    },
  },
};
</script>

<style  scoped>
</style>